//
//  KiwipenEvent.m
//  BlueToothDemo
//
//  Created by US bike on 2017/8/29.
//  Copyright © 2017年 奇异果. All rights reserved.
//

#import "KiwipenEvent.h"
#import "Tools.h"
#import "KiwipenProtocol.h"


@implementation KiwipenEvent


- (instancetype) initWithEventData:(NSData *) data {
    
    self = [super init];
    if (self) {
        
        [self setupEventWithData:data];
        
    }
    return self;
}


- (void) setupEventWithData:(NSData *) data {
    Byte* bytes = (Byte *) data.bytes;
    
    
    if ([data length] < 5) {
        NSLog(@"指令长度小于5个字节，无效命令。");
        _eventType = EventTypeError;
        return;
    }
    
    
    if (bytes[0] != CMD_Begin1 || bytes[1] != CMD_Begin2) {
        
        NSLog(@"指令起始码不匹配，无效命令");
        _eventType = EventTypeError;
        return;
    }
    
    if (bytes[data.length - 1] != CMD_END) {
        
        NSLog(@"指令结束码不匹配，无效命令");
        _eventType = EventTypeError;
        return;
    }
    
    Byte operator = bytes[2];
    Byte crc;
    //根据识别符判断命令类型
    if (operator == CMD_OPERATOR_BUTTON) {
        _eventType = EventTypeButton;
        NSLog(@"设备按键命令，App收到的数据");
        
        crc = bytes[5];
        if (crc != [Tools xorsBytes:bytes[2],bytes[3],bytes[4], nil]) {
            _eventType = EventTypeError;
            return;
        }
        
        if (bytes[3] == BUTTON_UP) {
            
            if (bytes[4] == BUTTON_FALLING_EDGE) {
                _eventDetailType = EventDetailTypeVALUE_UP_BUTTON_FALLING_EDGE;
            } else if (bytes[4] == BUTTON_RISING_EDGE) {
                _eventDetailType = EventDetailTypeVALUE_UP_BUTTON_RISING_EDGE;
            } else if (bytes[4] == BUTTON_SINGLE_CLICK) {
                _eventDetailType = EventDetailTypeVALUE_UP_BUTTON_SINGLE_CLICK;
            } else if (bytes[4] == BUTTON_DOUBLE_CLICK) {
                _eventDetailType = EventDetailTypeVALUE_UP_BUTTON_DOUBLE_CLICK;
            } else if (bytes[4] == BUTTON_LONG_PRESS) {
                _eventDetailType = EventDetailTypeVALUE_UP_BUTTON_LONG_PRESS;
            } else if (bytes[4] == BUTTON_LONG_RELEASE) {
                _eventDetailType = EventDetailTypeVALUE_UP_BUTTON_LONG_RELEASE;
            }
            
        } else if (bytes[3] == BUTTON_DOWN) {
            if (bytes[4] == BUTTON_FALLING_EDGE) {
                _eventDetailType = EventDetailTypeVALUE_DOWN_BUTTON_FALLING_EDGE;
            } else if (bytes[4] == BUTTON_RISING_EDGE) {
                _eventDetailType = EventDetailTypeVALUE_DOWN_BUTTON_RISING_EDGE;
            } else if (bytes[4] == BUTTON_SINGLE_CLICK) {
                _eventDetailType = EventDetailTypeVALUE_DOWN_BUTTON_SINGLE_CLICK;
            } else if (bytes[4] == BUTTON_DOUBLE_CLICK) {
                _eventDetailType = EventDetailTypeVALUE_DOWN_BUTTON_DOUBLE_CLICK;
            } else if (bytes[4] == BUTTON_LONG_PRESS) {
                _eventDetailType = EventDetailTypeVALUE_DOWN_BUTTON_LONG_PRESS;
            } else if (bytes[4] == BUTTON_LONG_RELEASE) {
                _eventDetailType = EventDetailTypeVALUE_DOWN_BUTTON_LONG_RELEASE;
            }
            
        } else if (bytes[3] == BUTTON_MID) {
            if (bytes[4] == BUTTON_FALLING_EDGE) {
                _eventDetailType = EventDetailTypeVALUE_MID_BUTTON_FALLING_EDGE;
            } else if (bytes[4] == BUTTON_RISING_EDGE) {
                _eventDetailType = EventDetailTypeVALUE_MID_BUTTON_RISING_EDGE;
            } else if (bytes[4] == BUTTON_SINGLE_CLICK) {
                _eventDetailType = EventDetailTypeVALUE_MID_BUTTON_SINGLE_CLICK;
            } else if (bytes[4] == BUTTON_DOUBLE_CLICK) {
                _eventDetailType = EventDetailTypeVALUE_MID_BUTTON_DOUBLE_CLICK;
            } else if (bytes[4] == BUTTON_LONG_PRESS) {
                _eventDetailType = EventDetailTypeVALUE_MID_BUTTON_LONG_PRESS;
            } else if (bytes[4] == BUTTON_LONG_RELEASE) {
                _eventDetailType = EventDetailTypeVALUE_MID_BUTTON_LONG_RELEASE;
            }
        }
        
    } else if (operator == CMD_OPERATOR_POWER) {

        crc = bytes[5];
        if (crc != [Tools xorsBytes:bytes[2],bytes[3],bytes[4], nil]) {
            _eventType = EventTypeError;
            return;
        }
        
        NSLog(@"电池电量命令");
        
        _eventType = EventTypePower;
        _eventData = [data subdataWithRange:NSMakeRange(3, 2)];
        
    } else if (operator == CMD_OPERATOR_LED) {
        
        NSLog(@"这里是LED命令， 不属于事件，这里不处理");
        _eventType = EventTypeError;
        return;
        
    } else if (operator == CMD_OPERATOR_UUID) {

        
        int uuidLength = 0xFF & bytes[3];
        Byte uuidCRC = bytes[4];
        for (int i = 1; i < uuidLength; i++) {
            uuidCRC = [Tools xorByteA:uuidCRC AndByteB:bytes[i + 4]];
        }
        
        crc = bytes[4 + uuidLength];
        if (crc != [Tools xorsBytes:bytes[2], bytes[3], uuidCRC, nil]) {
            _eventType = EventTypeError;
            return;
        }
        
        NSLog(@"读取UUID命令，App发送读请求");
        if (data.length == 5) {
            NSLog(@"APP发送读取UUID命令");
        } else {
            NSLog(@"APP接收读取UUDI的数据");
        }
        //设置数据
        _eventType = EventTypeUUID;
        _eventData = [data subdataWithRange:NSMakeRange(4, uuidLength)];

    }
}



- (NSString *) description {
    return [NSString stringWithFormat:@"kiwipenEvent  type = %lu, detailType = %lu, eventData = %@", (unsigned long)self.eventType,(unsigned long)self.eventDetailType,self.eventData];
}
@end
